<?php

// Keep this authentication include, needed to be able to store the session username!
require_once  __DIR__ . '/../authenticate.php';


if(isset($_POST))
{


$players = $_POST["players"];

$words = explode( ', ', $players);
$newplayers = [];
foreach ($words as $word) {	
	$newplayers[$word] = trim($word);	
}
$players = implode( ',', $newplayers);




$tags = $_POST["tags"];

$words = explode( ', ', $tags);
$newtags = [];
foreach ($words as $word) {	
	$newtags[$word] = trim($word);	
}
$tags = implode( ',', $newtags);



require_once __DIR__ . '/../BasketbalFunctions.php';

$basketbalfunctions = new BasketbalFunctions();

$username = $_SESSION['username'];

$added_by = $basketbalfunctions->GetUserkeyFromUsername($username);

 try{  

	 $connect = new PDO('mysql:host=localhost;dbname=basketbal','cas','cas');
	 $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	 $query = "INSERT INTO video_feedback (season, gamenumber, embed_code, time_start, time_end, type, title, comment, action_tags, players, added_by) VALUES (:season, :gamenumber, :embed_code, :time_start, :time_end, :type, :title, :comment, :action_tags, :players, :added_by)";
	 $statement = $connect->prepare($query);
	 $sql_status = $statement->execute(
	  array(
	   ':season'  => $_POST["season"],
	   ':gamenumber'  => $_POST["gamenumber"],
	   ':embed_code'  => $_POST["embed_code"],
	   ':time_start'  => $_POST['timestart'],
	   ':time_end'  => $_POST["timeend"],
	   ':type'  => $_POST["type"],
	   ':title'  => $_POST["title"],
	   ':comment'  => $_POST["comment"],
	   ':action_tags'  => $tags,
	   ':players'  => $players,
	   ':added_by' => $added_by
	  )
	 );
	 $output = '';
	 if ($sql_status) {
	 	  $output = '
	  <div class="alert alert-success">
	   Your data has been successfully saved into System
	  </div>
	  ';
	 } else {
	 	$output = "Something Wrong";
	 }

} catch (PDOException $e) {
    
    $output = $e->getMessage();
}

 echo $output;

}

?>